package edu.cuit.campus.web.controller;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Arrays;

/**
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/8/15 10:39
 */
@Controller
public class WelcomeController {

    @RequestMapping("/welcome")
    public String welcome(Model model){
        model.addAttribute("message", "welcome to springMVC!");
        return "welcome";
    }

    @RequestMapping(value = "/param/list", produces = "text/plain;charset=UTF-8")
    @ResponseBody
    public String listParam(@RequestBody Integer[] ids){
        return "数组(列表)请求参数:" + Arrays.toString(ids);
    }

    @ResponseBody
    @RequestMapping("/param/req") //不推荐,因为和servlet-api紧耦合
    public String getHttpServletRequestParameter(HttpServletRequest request){
        return request.getParameter("username");
    }

    @ResponseBody
    @RequestMapping("/param/context")
    public String getRequestContext(){
        //通过RequestContextHolder上下文对象可以获取web上下文对象
        var sra = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        var request = sra.getRequest();
        return request.getParameter("username");
    }
}
